<?php

/**
 * 首页
 * @copyright (c) 2013, Mr.Wang
 * @author Mr.Wang <281762391@qq.com>
 */
class UserAction extends BaseAction {

    function _initialize() {
        parent::_initialize();
        C("LAYOUT_ON", TRUE);
        if (format_int(session("uid")) == 0) {
            $this->error("请您先登录", U("Public/login"));
        }
    }

    /**
     * 生成SEO
     * @param $catid        栏目ID
     * @param $title        标题
     * @param $description  描述
     * @param $keyword      关键词
     */
    function seo($catid = '', $title = '', $description = '', $keyword = '') {
        if (!empty($title))
            $title       = strip_tags($title);
        if (!empty($description))
            $description = strip_tags($description);
        if (!empty($keyword))
            $keyword     = str_replace(' ', ',', strip_tags($keyword));

        $site = F("Config");

        $categorys      = F("Category");
        $cat            = $categorys[$catid];
        $cat['setting'] = unserialize($cat['setting']);

        $seo['site_title']  = $site['sitename'];
        $seo['keyword']     = !empty($keyword) ? $keyword : $site['sitekeywords'];
        $seo['description'] = isset($description) && !empty($description) ? $description : (isset($cat['setting']['meta_description']) && !empty($cat['setting']['meta_description']) ? $cat['setting']['meta_description'] : (isset($site['siteinfo']) && !empty($site['siteinfo']) ? $site['siteinfo'] : ''));
        $seo['title']       = (isset($title) && !empty($title) ? $title . ' - ' : '') . (isset($cat['setting']['meta_title']) && !empty($cat['setting']['meta_title']) ? $cat['setting']['meta_title'] . ' - ' : (isset($cat['catname']) && !empty($cat['catname']) ? $cat['catname'] . ' - ' : ''));
        foreach ($seo as $k => $v) {
            $seo[$k] = str_replace(array("\n", "\r"), '', $v);
        }
        return $seo;
    }

    Public function upload($savePath = './Public/Uploads/') {
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        if (strpos($savePath, ".") !== 0) {
            $savePath = "." . $savePath;
        }


        if (!is_dir($savePath)) {
            createDir($savePath);
        }
        $upload->savePath = $savePath; // 设置附件上传目录
        if (!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        } else {// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
            $list = array();
            foreach ($info as $value) {
                $list[$value['key']] = $value;
            }
            return $list;
        }
    }

    public function has_upload() {
        foreach ($_FILES as $value) {
            if ($value['size'] > 0) {
                return true;
            }
        }
        return false;
    }

}
